<!--
  *  员工 表单 弹窗
  * 
  * @Author:    1024创新实验室-主任：卓大 
  * @Date:      2022-08-08 20:46:18
  * @Wechat:    zhuda1024 
  * @Email:     lab1024@163.com 
  * @Copyright  1024创新实验室 （ https://1024lab.net ），Since 2012 
-->
<template>
  <a-drawer
    :title="form.id ? '编辑' : '添加'"
    :width="600"
    :visible="visible"
    :body-style="{ paddingBottom: '80px' }"
    @close="onClose"
    destroyOnClose
  >
    <a-form ref="formRef" :model="form" :rules="rules" layout="vertical">
      <a-form-item label="医馆编号" name="appid">
        <a-input v-model:value.trim="form.appid" placeholder="请输入医馆编号" />
      </a-form-item>
      <a-form-item label="医馆名称" name="name">
        <a-input v-model:value.trim="form.name" placeholder="请输入医馆名称" />
      </a-form-item>
	  <a-form-item label="医馆图片地址" name="pic">
	    <a-input v-model:value.trim="form.pic" placeholder="请输入医馆图片地址" />
	  </a-form-item>
      <a-form-item label="联系电话" name="tel">
        <a-input v-model:value.trim="form.tel" placeholder="请输入联系电话" />
      </a-form-item>
      <a-form-item label="医馆地址" name="addr">
        <a-input v-model:value.trim="form.addr" placeholder="请输入地址" />
      </a-form-item>
	 <!-- <a-form-item label="坐标名称" name="location_name">
	    <a-input v-model:value.trim="form.location_name" placeholder="请输入坐标名称" />
	  </a-form-item>
	  <a-form-item label="坐标地址" name="location">
	    <a-input v-model:value.trim="form.location" placeholder="请输入坐标地址" />
		<p class="hint">使用逗号分割</p>
	  </a-form-item> -->
      <a-form-item label="状态" name="status">
        <a-select v-model:value="form.status" placeholder="请选择状态">
          <a-select-option :value="0">启用</a-select-option>
          <a-select-option :value="1">禁用</a-select-option>
        </a-select>
      </a-form-item>
     
    </a-form>
    <div class="footer">
      <a-button style="margin-right: 8px" @click="onClose">取消</a-button>
      <a-button type="primary" style="margin-right: 8px" @click="onSubmit(false)">保存</a-button>
      <a-button v-if="!form.id" type="primary" @click="onSubmit(true)">保存并继续添加</a-button>
    </div>
  </a-drawer>
</template>
<script setup lang="ts">
  import { message } from 'ant-design-vue';
  import _ from 'lodash';
  import { nextTick, reactive, ref } from 'vue';
  import { roleApi } from '/@/api/system/role/role-api';
  import { businessesApi } from '/@/api/system/infirmary/businesses-api';
  import DepartmentTreeSelect from '/@/components/system/department-tree-select/index.vue';
  import SmartEnumSelect from '/@/components/framework/smart-enum-select/index.vue';
  import { GENDER_ENUM } from '/@/constants/common-const';
  import { regular } from '/@/constants/regular-const';
  import { SmartLoading } from '/@/components/framework/smart-loading';
import { smartSentry } from '/@/lib/smart-sentry';
  // ----------------------- 以下是字段定义 emits props ---------------------
  const departmentTreeSelect = ref();
  // emit
  const emit = defineEmits(['refresh', 'show-account']);

  // ----------------------- 显示/隐藏 ---------------------

  const visible = ref(false); // 是否展示抽屉
  // 隐藏
  function onClose() {
    reset();
	console.log("关闭了as")
    visible.value = false;
  }
  // 显示
  async function showDrawer(rowData) {
	console.log("关闭了as显示",rowData,!_.isEmpty(rowData))
    Object.assign(form, formDefault);
    if (rowData && !_.isEmpty(rowData)) {
      Object.assign(form, rowData);
    }else{
		Object.assign(form, {});
	}
    visible.value = true;
  }
	
  // ----------------------- 表单显示 ---------------------
  const formRef = ref(); // 组件ref
  // 添加的时候相关表单信息的是否显示的关键点
  const formDefault = {
    id: undefined,
	appid:undefined,
	name:undefined,
	pic:undefined,
	tel:undefined,
	addr:undefined,
	status:0,
  };

  let form = reactive(_.cloneDeep(formDefault));
  function reset() {
    Object.assign(form, formDefault);
    formRef.value.resetFields();
  }

  // ----------------------- 表单提交 ---------------------
  // 表单规则
  const rules = {
    appid: [
      { required: true, message: '渠道编号不能为空' },
      { max: 30, message: '渠道编号不能大于30个字符', trigger: 'blur' },
    ],
    name: [
      { required: true, message: '医馆名称不能为空' },
    ],
	tel: [
	  { required: true, message: '联系号码不能为空' },
	  { pattern: regular.tel, message: '请输入正确的手机号码', trigger: 'blur' },
	],
    status: [{ required: true, message: '状态不能为空' }],
  };

  // 校验表单
  function validateForm(formRef) {
    return new Promise((resolve) => {
      formRef
        .validate()
        .then(() => {
          resolve(true);
        })
        .catch(() => {
          resolve(false);
        });
    });
  }

  // 提交数据
  async function onSubmit(keepAdding) {
    let validateFormRes = await validateForm(formRef.value);
    if (!validateFormRes) {
      message.error('参数验证错误，请仔细填写表单数据!');
      return;
    }
    SmartLoading.show();
    if (form.id) {
      await updateBusinessesinfo(keepAdding);
    } else {
      await addBusinessesinfo(keepAdding);
    }
  }

  async function addBusinessesinfo(keepAdding) {
    try {
      let { data } = await businessesApi.addBusinessesinfo(form);
      message.success('添加成功');
      emit('show-account', form.loginName, data);
      if (keepAdding) {
        reset();
      } else {
        onClose();
      }
      emit('refresh');
    } catch (error) {
      // smartSentry.captureError(error);
    } finally {
      SmartLoading.hide();
    }
  }
  async function updateBusinessesinfo(keepAdding) {
    try {
      let result = await businessesApi.updateBusinessesinfo(form);
      message.success('更新成功');
      if (keepAdding) {
        reset();
      } else {
        onClose();
      }
      emit('refresh');
    } catch (error) {
      // smartSentry.captureError(error);
    } finally {
      SmartLoading.hide();
    }
  }

  // ----------------------- 以下是暴露的方法内容 ----------------------------
  defineExpose({
    showDrawer,
  });
</script>
<style scoped lang="less">
  .footer {
    position: absolute;
    right: 0;
    bottom: 0;
    width: 100%;
    border-top: 1px solid #e9e9e9;
    padding: 10px 16px;
    background: #fff;
    text-align: right;
    z-index: 1;
  }
  .hint {
    margin-top: 5px;
    color: #bfbfbf;
  }
</style>
